import express from 'express'
import http from 'http'
import socket from 'socket.io'

import { MessageType, EnterResult, UserInfo } from './models/socket-io-model'
import socket_init from "./socket-init"

const app = express();
const http_server = http.createServer(app);
const socket_io = socket.listen(http_server);

app.use("/app", express.static('app_dist'));
app.use("/pub", express.static('public'));

app.get('/', (req, res) => res.send('Hello World!'));

// socket 
let socket_index = 1;
let socket_dict: { [code: string]: number } = {};
let socket_list: { [index: number]: UserInfo, get_index(): number } = {
    get_index() { return socket_index++; }
};
Object.defineProperty(socket_list, "get_index", {
    enumerable: false,
    writable: false,
    value: () => socket_index++
});

// socket_io.on("connect", socket => {
//     // 先于 connection
// });

socket_io.on(
    "connection",
    socket_init(
        socket_io,
        socket_dict,
        socket_list
    )
);

http_server.listen(
    13001,
    () => console.log('http listening on port 13001!')
);